<?php
function getContactsByProgram($print=null) {
	global $dbname, $db, $db_selected;
	$contactString = '';
	
	if ($print == 1) {
		$cols = 3;
		$colWidth = '33%';
	} else {
		$cols = 2;
		$colWidth = '50%';
	}
	
	$sql = "SELECT * from casa_programs ORDER BY ProgramName ASC";
	$result = mysql_db_query($dbname,$sql);
	
	while ($value = mysql_fetch_assoc($result)) {
		$pID      = $value[PID];
		$pName    = stripslashes($value[ProgramName]);
		$pCounty  = stripslashes($value[County]);
		$pAddress = $value[Address];
		$pCity    = $value[City];
		$pZip     = $value[Zip];
		
		$contactString .= '<h1>'.$pName.'</h1>';
		
		$sql2 = "SELECT CU.UID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,CU.OfficeNo,CU.PositionID,C.CatName AS Position 
				FROM casa_users CU 
				LEFT JOIN casa_categories C ON CU.PositionID = C.CID
				WHERE 
				CU.ProgramID = '$pID' AND
				CU.Public = 1
				ORDER BY CU.NameLast";
		$result2 = mysql_db_query($dbname,$sql2);
		$rows2 = mysql_num_rows($result2);
		
		if ($rows2>0) {	
			$i=0;
			
			// Clear array from previous result set
			$userArray = array();
			while ($value2 = mysql_fetch_assoc($result2)) {
				$userArray[$i][0] = $value2[UID];
				$userArray[$i][1] = stripslashes($value2[NameFirst]);
				$userArray[$i][2] = stripslashes($value2[NameLast]);
				$userArray[$i][3] = $value2[Email];
				$userArray[$i][4] = $value2[Phone1];
				$userArray[$i][5] = $value2[Phone2];
				$userArray[$i][6] = $value2[OfficeNo];
				$userArray[$i][7] = $value2[PositionID];
				$userArray[$i][8] = $value2[Position];
				$userArray[$i][9] = $programID;
				$userArray[$i][11]= $value2['Public'];
				
				$i++;
			} // End WHILE
	
			$i=$i-1;
			$rows = ceil($i/$cols);
			$c=0;
						
			$contactString .= '<table style="width: 100%;">'.Chr(10);
			for($a=0;$a<=$rows;$a++) {		
				$contactString .= '<tr>';
				for($b=1;$b<=$cols;$b++) {
					$contactString .= '<td style="vertical-align: top; width: '.$colWidth.';">'.Chr(10);
					if (!empty($userArray[$c][0])) {						
						$contactString .= renderUserDetails($userArray[$c],'table');
					}
					$contactString .= '</td>'.Chr(10);
						
					$c++;
				}
				$contactString .= '</tr>';		
			}
			$contactString .= '</table>';
			
		} else {
			$contactString .= 'There are no contacts listed for this program yet.';
		}
	}
	
	return $contactString;
}

function getContactsByLastName() {
	global $dbname, $db, $db_selected;
	$contactString = '';
		
	$sql2 = "SELECT 
				CU.UID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,
				CU.OfficeNo,CU.PositionID,C.CatName AS Position,
				CP.ProgramName AS ProgramName
			FROM casa_users CU 
			LEFT JOIN casa_categories C ON CU.PositionID = C.CID
			LEFT JOIN casa_programs CP ON CP.PID = CU.ProgramID
			WHERE
			CU.Public = 1
			ORDER BY CU.NameLast";
	$result2 = mysql_db_query($dbname,$sql2);
	$rows2 = mysql_num_rows($result2);
	
	if ($rows2>0) {	
		$i=0;
		while ($value2 = mysql_fetch_assoc($result2)) {
			$userArray[$i][0] = $value2[UID];
			$userArray[$i][1] = stripslashes($value2[NameFirst]);
			$userArray[$i][2] = stripslashes($value2[NameLast]);
			$userArray[$i][3] = $value2[Email];
			$userArray[$i][4] = $value2[Phone1];
			$userArray[$i][5] = $value2[Phone2];
			$userArray[$i][6] = $value2[OfficeNo];
			$userArray[$i][7] = $value2[PositionID];
			$userArray[$i][8] = $value2[Position];
			$userArray[$i][9] = $programID;
			$userArray[$i][10]= $value2[ProgramName];
			$userArray[$i][12]= $value2[ProgramURL];
						
			$i++;
		} // End WHILE
		$i=$i-1;
		
		$contactString .= '<table class="dataTable" style="width: 100%;">';
		$contactString .= '<tr>';
		$contactString .= '<th>Name</th>';
		$contactString .= '<th>Position</th>';
		$contactString .= '<th>Program</th>';
		$contactString .= '<th>Email</th>';
		$contactString .= '<th>Phone 1</th>';
		$contactString .= '</tr>';
		
		for($a=0;$a<=$i;$a++) {	
			if (($a % 2) > 0) { $class = 'altRow'; }
			else { $class = ''; }
			$contactString .= renderUserDetails($userArray[$a],'grid',$class);
		}	
		$contactString .= '</table>';
	} else {
		$contactString .= 'There are no contacts listed for this program yet.';
	}
	
	return $contactString;
}

function getContacts($sortBy=null) {
	global $dbname, $db, $db_selected;
	$contactString = '';
	
	if (empty($sortBy)) {
		$sortBy = 'ProgramName';
	}
		
	$sql2 = "SELECT 
				CU.UID,CU.NameFirst,CU.NameLast AS NameLast,CU.Email,CU.Phone1,CU.Phone2,
				CU.OfficeNo,CU.PositionID,C.CatName AS Position,
				CP.ProgramName AS ProgramName,CP.URL AS ProgramURL
			FROM casa_users CU 
			LEFT JOIN casa_categories C ON CU.PositionID = C.CID
			LEFT JOIN casa_programs CP ON CP.PID = CU.ProgramID
			WHERE
			CU.Public = 1
			ORDER BY $sortBy";
	$result2 = mysql_db_query($dbname,$sql2);
	$rows2 = mysql_num_rows($result2);
	
	if ($rows2>0) {	
		$i=0;
		while ($value2 = mysql_fetch_assoc($result2)) {
			$userArray[$i][0] = $value2[UID];
			$userArray[$i][1] = stripslashes($value2[NameFirst]);
			$userArray[$i][2] = stripslashes($value2[NameLast]);
			$userArray[$i][3] = $value2[Email];
			$userArray[$i][4] = $value2[Phone1];
			$userArray[$i][5] = $value2[Phone2];
			$userArray[$i][6] = $value2[OfficeNo];
			$userArray[$i][7] = $value2[PositionID];
			$userArray[$i][8] = $value2[Position];
			$userArray[$i][9] = $programID;
			$userArray[$i][10]= $value2[ProgramName];
			$userArray[$i][12]= $value2[ProgramURL];
						
			$i++;
		} // End WHILE
		$i=$i-1;
		
		$contactString .= '<table class="dataTable" style="width: 100%;">';
		$contactString .= '<tr>';
		$contactString .= '<th>Name</th>';
		$contactString .= '<th>Position</th>';
		$contactString .= '<th>Program</th>';
		$contactString .= '<th>Email</th>';
		$contactString .= '<th>Phone 1</th>';
		$contactString .= '</tr>';
		
		for($a=0;$a<=$i;$a++) {	
			if (($a % 2) > 0) { $class = 'altRow'; }
			else { $class = ''; }
			$contactString .= renderUserDetails($userArray[$a],'grid',$class);
		}	
		$contactString .= '</table>';
	} else {
		$contactString .= 'There are no contacts listed for this program yet.';
	}
	
	return $contactString;
}

function renderUserDetails($userArray,$layout=null,$rowClass=null,$mode=null) {
	$returnString = '';
	
	$uID         = $userArray[0];
	$nameFirst   = $userArray[1];
	$nameLast    = $userArray[2];
	$email       = $userArray[3];
	$phone1      = $userArray[4];
	$phone2      = $userArray[5];
	$officeNo    = $userArray[6];
	$positionID  = $userArray[7];
	$position    = $userArray[8];
	$programID   = $userArray[9];
	$programName = $userArray[10];
	$public      = $userArray[11];
	$programURL  = $userArray[12];
	
	if ($officeNo == 0) {
		$officeNo = '';
	}
	if ($phone1 == 0) {
		$phone1 = '';
	}
	
	if ( empty($layout) || ($layout == 'table') ) {
		$returnString .= '<div id="contactContainer'.$uID.'">';
		$returnString .= '<div style="padding: 2px; background: #EBF4FD;">';
			if ($mode == 'edit') {
				$returnString .= '<a href="#" onClick="confirmDelete('.$uID.',\'contact\'); return false;" title="Delete this contact"><img src="images/iconDeleteSmall.gif" style="border: none;" /></a>&nbsp;';
				$returnString .= '<a href="#" onClick="editProgramContact('.$uID.'); return false;" title="Edit this contact"><img src="images/iconEditSmall.gif" style="border: none;" /></a>&nbsp;';
			}
			$returnString .= '<b>'.$nameFirst.' '.$nameLast.'</b>';
		$returnString .= '</div>';
		$returnString .= '<div style="padding: 2px;">';
		if (!empty($position)) {
			$returnString .= $position.'<br />';
		}
		if (!empty($email)) {
			$returnString .= '<a href="mailto:'.$email.'">Email '.$nameFirst.' '.$nameLast.'</a><br />';
		}
		if (!empty($phone1)) {
			$returnString .= 'Office: '.$phone1.'<br />';
		}
		if (!empty($phone2)) {
			$returnString .= 'Mobile, Other: '.$phone2.'<br />';
		}
		if (!empty($officeNo)) {
			$returnString .= 'Office Number: '.$officeNo.'<br />';
		}
		if ($public == 1) {
			$returnString .= '<div style="margin-top: 3px;"><img src="images/iconCheckGreenOn.gif" /> Public Contact</div>';
		}
		$returnString .= '</div></div><br />';
		$returnString .= '<div id="contactContainerTemp'.$uID.'" style="display: none;"></div>';
	} elseif ($layout == 'grid') {
		$returnString .= '<tr id="contactContainer'.$uID.'" class="'.$rowClass.'">';
		$returnString .= '<td>'.$nameLast.', '.$nameFirst.'</td>';
		$returnString .= '<td>'.$position.'</td>';
		$returnString .= '<td>';
		
		if (!empty($programURL)) {
			$returnString .= '<a href="'.$programURL.'" target="_blank">';
		}
		
		$returnString .= $programName;

		if (!empty($programURL)) {
			$returnString .= '</a>';
		}
		
		$returnString .= '</td>';
		$returnString .= '<td><a href="mailto:'.$email.'">Email '.$nameFirst.' '.$nameLast.'</a></td>';
		$returnString .= '<td>'.$phone1.'</td>';
		$returnString .= '</tr>';		
	}
	
	return $returnString;
}
?>